package cn.edu.ctbu.lddweb.controller;

import cn.edu.ctbu.lddweb.pojo.Class;
import cn.edu.ctbu.lddweb.pojo.Grade;
import cn.edu.ctbu.lddweb.pojo.Student;
import cn.edu.ctbu.lddweb.service.ClassService;
import cn.edu.ctbu.lddweb.service.GradeService;
import cn.edu.ctbu.lddweb.service.LogonService;
import cn.edu.ctbu.lddweb.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * 学生管理Controller
 */
@Controller
public class StudentController {
    @Autowired
    private StudentService studentService;

    @Autowired
    private ClassService classService;

    @Autowired
    private LogonService logonService;

    @Autowired
    private GradeService gradeService;

    //学生管理列表页面
    @RequestMapping("/student/index")
    public String index(Model model, ModelMap map,Student student){
        List<Student> list = studentService.selectList(student);
        model.addAttribute("student",list);

        //年级下拉
        Grade grade = new Grade();
        List<Grade> list1 = gradeService.selectList(grade);
        model.addAttribute("grade",list1);

        //班级下拉
        Class aclass = new Class();
        List<Class> list2 = classService.selectList(aclass);
        model.addAttribute("class",list2);
        return "/student/index";
    }

    //搜索/重置
    @RequestMapping("/selectStudent")
    public String selectStudent(@RequestParam(value = "username",required = false) String username,@RequestParam(value = "className",required = false) String className,@RequestParam(value = "gradeName",required = false) String gradeName, Model model){
        Student student = new Student();
        student.setUsername(username);
        student.setClassName(className);
        student.setGradeName(gradeName);
        List<Student> list = studentService.selectList(student);
        model.addAttribute("student",list);

        //年级下拉
        Grade grade = new Grade();
        List<Grade> list1 = gradeService.selectList(grade);
        model.addAttribute("grade",list1);

        //班级下拉
        Class aclass = new Class();
        List<Class> list2 = classService.selectList(aclass);
        model.addAttribute("class",list2);
        return "/student/index";
    }

    //新增页面
    @RequestMapping("/student/insert")
    public String insert(Model model){
        //年级下拉
        Grade grade = new Grade();
        List<Grade> list1 = gradeService.selectList(grade);
        model.addAttribute("grade",list1);

        //班级下拉
        Class aclass = new Class();
        List<Class> list2 = classService.selectList(aclass);
        model.addAttribute("class",list2);
        return "/student/insert";
    }

    //新增操作
    @RequestMapping("/insertStudent")
    public String insertStudent(Student student){
        //判断学生是否已存在
        List<Student> list = studentService.selectList(student);
        if(list.size()!=0){
            return "error111";
        }
        studentService.insertStudent(student);
        return "/student/index";
    }


    //修改
    @RequestMapping("/student/edit")
    public String edit(int id,Model model){
        Student student = studentService.findID(id);
        model.addAttribute("student",student);

        //年级下拉
        Grade grade = new Grade();
        List<Grade> list1 = gradeService.selectList(grade);
        model.addAttribute("grade",list1);

        //班级下拉
        Class aclass = new Class();
        List<Class> list2 = classService.selectList(aclass);
        model.addAttribute("class",list2);
        return "/student/edit";
    }

    //修改操作
    @RequestMapping("/updateStudent")
    public String updateStudent(Student student){
        Boolean boo = studentService.checkStudentUnique(student);
        if(boo){
            return "error111";
        }
        studentService.updateStudent(student);
        return "/student/index";
    }

    //删除
    @RequestMapping("/deleteStudent")
    public String deleteStudent(int id){
        studentService.deleteStudent(String.valueOf(id));
        return "/student/index";
    }
}
